home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / dviware / dvibook / libtex / getopt.c < prev    next >
C/C++ Source or Header  |  1994-03-18  |  1KB  |  70 lines

  1. /*
  2.  * Copyright (c) 1987, 1989 University of Maryland
  3.  * Department of Computer Science.  All rights reserved.
  4.  * Permission to copy for any purpose is hereby granted
  5.  * so long as this copyright notice remains intact.
  6.  */
  7.  
  8. /*
  9.  * getopt - get option letter from argv
  10.  * (From Henry Spencer @ U of Toronto Zoology, slightly edited)
  11.  */
  12.  
  13. #include <stdio.h>
  14.  
  15. char    *optarg;    /* Global argument pointer. */
  16. int    optind;        /* Global argv index. */
  17.  
  18. static char *scan;    /* Private scan pointer. */
  19.  
  20. extern char *index();
  21.  
  22. int
  23. getopt(argc, argv, optstring)
  24.     register int argc;
  25.     register char **argv;
  26.     char *optstring;
  27. {
  28.     register int c;
  29.     register char *place;
  30.  
  31.     optarg = NULL;
  32.     if (scan == NULL || *scan == 0) {
  33.         if (optind == 0)
  34.             optind++;
  35.         if (optind >= argc || argv[optind][0] != '-' ||
  36.             argv[optind][1] == 0)
  37.             return (EOF);
  38.         if (strcmp(argv[optind], "--") == 0) {
  39.             optind++;
  40.             return (EOF);
  41.         }
  42.         scan = argv[optind] + 1;
  43.         optind++;
  44.     }
  45.     c = *scan++;
  46.     place = index(optstring, c);
  47.  
  48.     if (place == NULL || c == ':') {
  49.         fprintf(stderr, "%s: unknown option -%c\n", argv[0], c);
  50.         return ('?');
  51.     }
  52.     place++;
  53.     if (*place == ':') {
  54.         if (*scan != '\0') {
  55.             optarg = scan;
  56.             scan = NULL;
  57.         } else {
  58.             if (optind >= argc) {
  59.                 fprintf(stderr,
  60.                     "%s: missing argument after -%c\n",
  61.                     argv[0], c);
  62.                 return ('?');
  63.             }
  64.             optarg = argv[optind];
  65.             optind++;
  66.         }
  67.     }
  68.     return (c);
  69. }
  70.